# -*- coding: utf-8 -*-
"""
场景数据加载器
"""

from typing import Any, Dict, List, Tuple
from pathlib import Path
import json


class ScenesLoader:
    """场景数据加载器"""

    @staticmethod
    def load(json_path: Path) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:
        """
        加载场景JSON文件

        Args:
            json_path: JSON文件路径

        Returns:
            (元数据, 场景列表)
        """
        with open(json_path, "r", encoding="utf-8") as f:
            data = json.load(f)

        scenes = data.get("scenes", [])

        return data, scenes

    @staticmethod
    def save(data: Dict[str, Any], output_path: Path) -> None:
        """
        保存场景数据

        Args:
            data: 场景数据
            output_path: 输出路径
        """
        output_path.parent.mkdir(parents=True, exist_ok=True)

        with open(output_path, "w", encoding="utf-8") as f:
            json.dump(data, f, ensure_ascii=False, indent=2)

    @staticmethod
    def concat_narration(scenes: List[Dict[str, Any]]) -> str:
        """
        合并所有场景的narration

        Args:
            scenes: 场景列表

        Returns:
            合并后的文本
        """
        texts = []
        for s in scenes:
            t = (s.get("narration") or "").strip()
            if t:
                texts.append(t)
        return "\n".join(texts).strip()
